package com.itheima.product.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.pojo.Product;
import com.itheima.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {


    @Autowired
    private ProductService productService;

    /**
     * 查询全部数据*
     * @return
     */
    @GetMapping
    public   List<Product> findAll() {
        List<Product> productList = productService.findAll();
        return productList;
    }

    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Product product = productService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", product);
    }


    /***
     * 新增数据
     * @param product
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Product product) {
        productService.add(product);
        return new Result(true, StatusCode.OK, "添加成功");
    }


    /***
     * 修改数据
     * @param product
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Product product, @PathVariable Integer id) {
        product.setId(id);
        productService.update(product);
        return new Result(true, StatusCode.OK, "修改成功");
    }


    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id) {
        productService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search")
    public Result findList(@RequestParam Map searchMap) {
        List<Product> list = productService.findList(searchMap);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Product> pageList = productService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    @GetMapping("/updateScore/{pid}")
    public Result updateScore(@PathVariable("pid") Integer pid) {
        productService.updateScore(pid);
        return new Result(true, StatusCode.OK, "更新商家星级成功");
    }
    @RequestMapping("/count")
    public Result count() {
        int count = productService.count();
        return new Result(true, StatusCode.OK, "查询数量成功", count);
    }
}
